<template>
  <div style="width:100%">
    <div style="text-align:left;line-height:30px;">
      <div style="border-bottom: 1px solid #dddddd">
        <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" plain @click="showOrganizationUserAddTableDialog" v-show="permissions.indexOf('fmOrganizationUserAdd')>0" >添加用户</el-button>
        <el-button type="danger" size="mini" icon="el-icon-remove-outline" plain @click="showOrganizationUserDeleteConfirm" v-show="permissions.indexOf('fmOrganizationUserDelete')>0">移除用户</el-button>
        <el-button type="success" size="mini" icon="el-icon-plus" plain @click="showOrganizationUserAddNewUserTableDialog" v-show="permissions.indexOf('fmOrganizationUserAddNew')>0">新建用户</el-button>
        <el-button type="success" size="mini" icon="el-icon-success" plain @click="showOrganizationUserStartUserConfirm" v-show="permissions.indexOf('fmOrganizationUserEnable')>0">启用用户</el-button>
        <el-button type="danger" size="mini" icon="el-icon-error" plain @click="showOrganizationUserStopUserConfirm"  v-show="permissions.indexOf('fmOrganizationUserDisable')>0">停用用户</el-button>
        <el-button type="success" size="mini" icon="el-icon-star-on" plain @click="showOrganizationUserSetManagerConfirm"  v-show="permissions.indexOf('fmOrganizationUserSetManager')>0">设置管理员</el-button>
        <el-button type="warning" size="mini" icon="el-icon-star-off" plain @click="showOrganizationUserCancelManagerConfirm"  v-show="permissions.indexOf('fmOrganizationUserCancelManager')>0">取消管理员</el-button>
        <el-button type="info" size="mini" icon="el-icon-d-arrow-right" plain @click="userToArganization"  v-show="permissions.indexOf('fmOrganizationUserToOrganization')>0">子组织列表</el-button>
      </div>
    </div>
    <div style="height:10px">

    </div>
    <yfu-ptable :param="param" v-on:getCheckedRows="getRows" ref="demoTable1"></yfu-ptable>

    <organizationUserAddTable ref="organizationUserAddTableRef" v-on:on-refresh-change="refreshOrganizationUserTable"></organizationUserAddTable>
    <organization-user-addnew-user-dialog ref="organizationUserAddnewUserDialogRef" v-on:on-refresh-change="refreshOrganizationUserTable"></organization-user-addnew-user-dialog>
  </div>
</template>
<script >
  import { getOrganizationUserListRemote, changeUserToAdminRemote, removeUserOrganizationRemote } from '@/view/api/organization.js'
  import store from '@/store'
  import organizationUserAddTable from '@/view/organization/organizationUserAddTable.vue'
  import organizationUserAddnewUserDialog from '@/view/organization/organizationUserAddnewUserDialog.vue'
  import { yfuConfirm, yfuErrorToast } from '@/utils/yfuMessage.js'
  import { updateUserStatus } from '@/view/api/userManager.js'
  import {yfuPost} from '@/utils/request.js'
  import getPermissionByMenuId from '@/utils/MenuPermissionUtils.js'
  export default {
    components: {
      'organizationUserAddTable': organizationUserAddTable,
      'organizationUserAddnewUserDialog': organizationUserAddnewUserDialog
    },
    props: ['menuId'],
    data () {
      return {
        permissions: '',
        organizationId: 0,
        checkedrows: [],
        param: {
          permissions: '',
          showBox: true,
          baseURL: process.env.BASE_API,
          url: getOrganizationUserListRemote.url,
          urlParam: {
            id: store.getters.currentOrganizationId
          },
          showToolButton: true,
          toolSearchSpan: 20,
          toolSearchItems: [
            {
              label: '用户名',
              prop: 'userName',
              type: 'input',
              span: 6
            }, {
              label: '真实姓名',
              prop: 'trueName',
              type: 'input',
              span: 6
            }
          ],
          showOperateRow: false,
          rows: [
            {
              prop: 'userName',
              label: '姓名',
              width: '100px'
            },
            {
              prop: 'trueName',
              label: '真实姓名',
              width: '100px'
            },
            {
              prop: 'status',
              label: '状态',
              width: '100px',
              formatter: function (row, column) {
                if (row.status === 0) {
                  return '已删除'
                } else if (row.status === 1) {
                  return '已启用'
                } else if (row.status === 2) {
                  return '已停用'
                }
              }
            },
            {
              prop: 'isAdmin',
              label: '是否管理员',
              width: '100px',
              formatter: function (row, column) {
                if (row.isAdmin === 0) {
                  return '否'
                } else if (row.isAdmin === 1) {
                  return '是'
                } else {
                  return '未知'
                }
              }
            },
//            {
//              prop: 'roles',
//              label: '所属角色',
//              width: '200px'
//            },
            {
              prop: 'createUser',
              label: '创建人',
              width: '100px'
            }, {
              prop: 'createTime',
              label: '创建时间'
            }
          ]
        }
      }
    },
    // 进入时初始化TABLE
    created () {
      let cThis = this
      this.organizationId = store.getters.currentOrganizationId

      getPermissionByMenuId(this.sessiongetJSON('user').id, this.menuId, function (permissionString) {
        cThis.permissions = permissionString
      })
    },
    methods: {
      // 得到所有选中行
      getRows (row) {
        this.checkedrows = row
      },
      // 刷新TABLE界面
      refresh (item) {
        this.$refs.demoTable1.refreshTableClearSearch(item)
      },
      tableSearch () {
        this.refresh()
      },
      // 去组织列
      userToArganization () {
        let cThis = this
        cThis.$emit('userToArganization')
      },
      // 展示添加用户
      showOrganizationUserAddTableDialog () {
        this.$refs.organizationUserAddTableRef.openOrganizationUserAddTableDialog()
      },
      // refreshOrganizationUserTable
      refreshOrganizationUserTable (item) {
        // 刷新表
        this.refresh(item)
      },
      // 展示添加用户
      showOrganizationUserAddNewUserTableDialog () {
        this.$refs.organizationUserAddnewUserDialogRef.createUserDialog()
      },
      showOrganizationUserDeleteConfirm () {
        let cThis = this
        if (this.checkedrows == null || this.checkedrows.length === 0) {
          yfuErrorToast('请选中一行')
          return
        }
        let userIds = ''
        for (let row of this.checkedrows) {
          userIds += row.id + ','
        }
        userIds = userIds.substring(0, userIds.length - 1)
        let oId = this.$store.getters.currentOrganizationId
        let param = {}
        param.userIds = userIds
        param.groupids = oId
        yfuConfirm('是否从组织中移除？', () => {
          yfuPost(process.env.BASE_API, removeUserOrganizationRemote.url, param, function () {
            cThis.refresh()
          })
        })
      },
      showOrganizationUserStartUserConfirm () {
        let cThis = this
        if (this.checkedrows == null || this.checkedrows.length === 0) {
          yfuErrorToast('请选中一行')
          return
        }
        let ids = []
        for (let element of this.checkedrows) {
          if (element.status === 1) {
            yfuErrorToast(`${element.userName}为启用用状态`)
            return
          }
          ids.push(element.id)
        }
        yfuConfirm('是否启用用户？', () => {
          console.log(this.checkedrows)

          let params = {}
          params.userIds = ids.join()
          params.status = 1
          updateUserStatus(params,
            function (data) {
              console.log(data)
              cThis.refresh()
              console.log('启用成功!')
            },
            function (error) {
              console.log(error)
            }
          )
        })
      },
      showOrganizationUserStopUserConfirm () {
        let cThis = this
        if (this.checkedrows == null || this.checkedrows.length === 0) {
          yfuErrorToast('请选中一行')
          return
        }
        let ids = []
        for (let element of this.checkedrows) {
          if (element.status === 2) {
            yfuErrorToast(`${element.userName}为停用状态`)
            return
          }
          ids.push(element.id)
        }
        yfuConfirm('是否停用用户？', () => {
          let params = {}
          params.userIds = ids.join()
          params.status = 2
          updateUserStatus(params,
            function (data) {
              console.log(data)
              cThis.refresh()
              console.log('停用成功!')
            },
            function (error) {
              console.log(error)
            }
          )
        })
      },
      showOrganizationUserSetManagerConfirm () {
        let cThis = this
        if (this.checkedrows == null || this.checkedrows.length === 0) {
          yfuErrorToast('请选中一行')
          return
        }
        let userIds = ''
        for (let row of this.checkedrows) {
          if (row.isAdmin === 1) {
            yfuErrorToast('用户' + row.userName + '是管理员，不能重复设置')
            return
          }
          userIds += row.id + ','
        }
        userIds = userIds.substring(0, userIds.length - 1)
        let oId = this.$store.getters.currentOrganizationId
        let param = {}
        param.userIds = userIds
        param.groupids = oId
        param.isAdmin = 1
        yfuConfirm('是否设置为组织管理员？', () => {
          yfuPost(process.env.BASE_API, changeUserToAdminRemote.url, param, function () {
            cThis.refresh()
          })
        })
      },
      showOrganizationUserCancelManagerConfirm () {
        let cThis = this
        if (this.checkedrows == null || this.checkedrows.length === 0) {
          yfuErrorToast('请选中一行')
          return
        }
        let userIds = ''
        for (let row of this.checkedrows) {
          if (row.isAdmin === 0) {
            yfuErrorToast('用户' + row.userName + '不是管理员，无法取消')
            return
          }
          userIds += row.id + ','
        }
        userIds = userIds.substring(0, userIds.length - 1)
        let oId = this.$store.getters.currentOrganizationId
        let param = {}
        param.userIds = userIds
        param.groupids = oId
        param.isAdmin = 0
        yfuConfirm('是否取消组织管理员？', () => {
          yfuPost(process.env.BASE_API, changeUserToAdminRemote.url, param, function () {
            cThis.refresh()
          })
        })
      }
    }
  }
</script>
<style scoped>

.organizationTableHeadCss {
  background-color: gray
}

</style>
